//
//  AppDelegate.m
//  GiftSay
//
//  Created by dllo on 16/1/6.
//  Copyright © 2016年 liyinkai. All rights reserved.
//

#import "AppDelegate.h"
#import "HomeViewController.h"
#import "HotViewController.h"
#import "CatagoryViewController.h"
#import "MineViewController.h"
#import "UMSocial.h"
#import "LYKRootViewController.h"
#import "DataBaseHandle.h"

#import "Reachability.h"

//#import "UMSocialWechatHandler.h"
@interface AppDelegate ()<delegate>
@property (nonatomic, retain) UITabBarController *TabBarController;

@property (nonatomic,retain) Reachability *network;

@end

@implementation AppDelegate


- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
    // Override point for customization after application launch.
    
    
    [[DataBaseHandle shareDataBase] openDataBase];
    [[DataBaseHandle shareDataBase] createMealsTable];
    [[DataBaseHandle shareDataBase] closeDatabase];
    
#pragma mark 夜间
    
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(nightModeOpen) name:@"NightModeOpen" object:nil];
    
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(nightModeOff) name:@"NightModeOff" object:nil];
    
    
    
    _window = [[UIWindow alloc] initWithFrame:[UIScreen mainScreen].bounds];
    HomeViewController *homeViewController = [[HomeViewController alloc] init];
    UINavigationController *homeNavigationController = [[UINavigationController alloc] initWithRootViewController:homeViewController];
    homeNavigationController.tabBarItem.title = @"首页";
    
    homeNavigationController.tabBarItem.image = [UIImage imageNamed:@"home_day_unselect"];
    homeNavigationController.tabBarItem.selectedImage = [UIImage imageNamed:@"home_day_select"];
    
    
    HotViewController *hotViewController = [[HotViewController alloc] init];
    UINavigationController *hotNavigationController = [[UINavigationController alloc] initWithRootViewController:hotViewController];
    hotNavigationController.tabBarItem.title = @"热门";
    
    hotNavigationController.tabBarItem.image = [UIImage imageNamed:@"hot_day_unselect"];
    hotNavigationController.tabBarItem.image = [UIImage imageNamed:@"hot_day_select"];
    
    
    CatagoryViewController *catagoryViewController = [[CatagoryViewController alloc] init];
    UINavigationController *catagoryNavigationController = [[UINavigationController alloc] initWithRootViewController:catagoryViewController];
    catagoryNavigationController.tabBarItem.title = @"分类";
    catagoryNavigationController.tabBarItem.image = [UIImage imageNamed:@"catagory_day_unselect"];
    catagoryNavigationController.tabBarItem.selectedImage = [UIImage imageNamed:@"catagory_day_select"];
    
    MineViewController *mineViewController = [[MineViewController alloc] init];
    UINavigationController *mineNavigationController = [[UINavigationController alloc] initWithRootViewController:mineViewController];
    mineNavigationController.tabBarItem.title = @"我的";
    mineNavigationController.tabBarItem.image = [UIImage imageNamed:@"mine_day_unselect"];
    mineNavigationController.tabBarItem.selectedImage = [UIImage imageNamed:@"mine_day_select"];
    
    
    self.TabBarController = [[UITabBarController alloc] init];
    
    if ([[NSUserDefaults standardUserDefaults] boolForKey:@"NightMode"] == YES) {
        self.TabBarController.tabBar.barTintColor = kNightModeNavigationColor;
    } else {
        self.TabBarController.tabBar.barTintColor = [UIColor whiteColor];
    }
    
    
    
    self.TabBarController.tabBar.tintColor = [UIColor redColor];
    self.TabBarController.viewControllers = @[homeNavigationController, hotNavigationController, catagoryNavigationController, mineNavigationController];
    
//    _window.rootViewController = self.TabBarController;
    if ( [[[NSUserDefaults standardUserDefaults] objectForKey:@"name"] isEqualToString:@"1"]) {
        [self gotoMain];
    }else {
        [self makeLauchView];
    }
    
    //判断网络请求
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(netChange) name:kReachabilityChangedNotification object:nil];
    self.network = [Reachability reachabilityForInternetConnection];
    
    [self connectUrl];
    
    [self.network startNotifier];
    
    _window.rootViewController = self.TabBarController;
    //友盟分享
    [UMSocialData setAppKey:@"569780d7e0f55a798200253a"];
    //微信分享
//    [UMSocialWechatHandler setWXAppId:<#(NSString *)#> appSecret:<#(NSString *)#> url:<#(NSString *)#>]
    return YES;
}


- (void) makeLauchView {
    LYKRootViewController *appView = [[LYKRootViewController alloc] init];
    appView.mydelegate = self;
    _window.rootViewController = appView;
}

- (void)pushOtherView {
    if (! [[NSUserDefaults standardUserDefaults] objectForKey:@"name"]) {
        [[NSUserDefaults standardUserDefaults] setObject:@"1" forKey:@"name"];
    }
    _window.rootViewController = self.TabBarController;
    //    _window.rootViewController = self.tabBarController;
}

- (void)gotoMain{
    
    _window.rootViewController = self.TabBarController;
}

- (void)netChange
{
    [self connectUrl];
}

- (void)connectUrl
{
    
    //对连接改变做出响应处理动作
    
    NetworkStatus status = [self.network currentReachabilityStatus];
    
    //如果没有连接到网络就弹出提醒实况
    
    if (status == NotReachable) {
        UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"网络连接异常" message:@"请检查网络连接信息" delegate:nil cancelButtonTitle:@"确定" otherButtonTitles:nil, nil];
        
        [alert show];
    }
    
    else if (status == ReachableViaWiFi){
        UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"现在连着Wi-Fi呢～" message:@"玩吧～" delegate:nil cancelButtonTitle:@"确定" otherButtonTitles:nil, nil];
        
        [alert show];
        
    }
    
    else if (status == ReachableViaWWAN){
        UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"现在连接数据流量" message:@"注意流量流失" delegate:nil cancelButtonTitle:@"确定" otherButtonTitles:nil, nil];
        [alert show];
    }
    
}


- (void)nightModeOpen {
    self.TabBarController.tabBar.barTintColor = kNightModeNavigationColor;
}
- (void)nightModeOff {
    self.TabBarController.tabBar.barTintColor = [UIColor whiteColor];
}



- (void)applicationWillResignActive:(UIApplication *)application {
    // Sent when the application is about to move from active to inactive state. This can occur for certain types of temporary interruptions (such as an incoming phone call or SMS message) or when the user quits the application and it begins the transition to the background state.
    // Use this method to pause ongoing tasks, disable timers, and throttle down OpenGL ES frame rates. Games should use this method to pause the game.
}

- (void)applicationDidEnterBackground:(UIApplication *)application {
    // Use this method to release shared resources, save user data, invalidate timers, and store enough application state information to restore your application to its current state in case it is terminated later.
    // If your application supports background execution, this method is called instead of applicationWillTerminate: when the user quits.
}

- (void)applicationWillEnterForeground:(UIApplication *)application {
    // Called as part of the transition from the background to the inactive state; here you can undo many of the changes made on entering the background.
}

- (void)applicationDidBecomeActive:(UIApplication *)application {
    // Restart any tasks that were paused (or not yet started) while the application was inactive. If the application was previously in the background, optionally refresh the user interface.
}

- (void)applicationWillTerminate:(UIApplication *)application {
    // Called when the application is about to terminate. Save data if appropriate. See also applicationDidEnterBackground:.
}

@end
